/**
 * @(#) AddressProxy.as
 *
 * @date		2011. 01. 04.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	 연말정산  - Proxy
 * 				 연말정산 주소 수정및조회
 */
package com.skt.common_hr.yearadjustment.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.yearadjustment.model.vo.AddressVO;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	

	public class AddressProxy extends CommonProxy
	{
		public static const NAME:String = "AddressProxy";
		
		public static const LOAD_ADDRESS_COMPLETE:String 	= "loadAddressComplete";
		public static const LOAD_ADDRESS_FAILED:String 		= "loadAddressFailed";
		public static const SAVE_ADDRESS_COMPLETE:String 	= "saveAddressComplete";
		public static const SAVE_ADDRESS_FAILED:String 		= "saveAddressFailed";
		
		public var msgStr:String;
		public var addressData:AddressVO = new AddressVO();
		

		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function AddressProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function load(pernr:String, year:String):void
		{
			var param:Object = new Object();
			param["I_YEAR"] = year;
			param["I_PERNR"] = pernr;
			param["I_PGBN"] = "1";		//1:조회, 2:저장

			loadData("Z_HR_0302", param, result, fault);
		}
		
		public function save(pernr:String, year:String, addr:Array):void{
			var param:Object = new Object();
			param["I_YEAR"] = year;
			param["I_PERNR"] = pernr;
			param["I_PGBN"] = "2";		//1:조회, 2:저장
			
			var tables:Object = new Object();
			tables["I_ADD"] = addr;
			saveData("Z_HR_0302", param, tables, saveResult, saveFault); 
		}

		public function newSave(pernr:String, year:String, addr:Array, type:String):void{
			var param:Object = new Object();
			param["I_PERNR"] = pernr;
			param["I_DATUM"] = year;
			param["I_SUBTY"] = type;	//1:본적, 2:현주소, 3:주민등록주소
			
			
			var tables:Object = new Object();
			tables["I_ADD"] = addr;
			saveData("Z_HR_0010", param, tables, saveResult, fault);  
		} 		

		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function result(event:ResultEvent):void
		{
			if(retCode==0) {  
				addressData = parseAddress(event.result.I_ADD);
				facade.sendNotification(LOAD_ADDRESS_COMPLETE, this);
				//facade.sendNotification(AddressProxy.SAVE_ADDRESS_COMPLETE, this); 
				
				
			}
			else {
				msgStr = event.result["RETTEXT"];
				facade.sendNotification(LOAD_ADDRESS_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function fault(event:FaultEvent):void
		{
			msgStr = event.fault.faultString;
			facade.sendNotification(LOAD_ADDRESS_FAILED, this);
		}
		
		//저장 완료
		public function saveResult(event:ResultEvent):void
		{
			if(retCode==0) {  
				facade.sendNotification(SAVE_ADDRESS_COMPLETE, this);
			}
			else {
				msgStr = event.result["RETTEXT"];
				facade.sendNotification(SAVE_ADDRESS_FAILED, this);
			}
		}
		
		//저장실패
		private function saveFault(event:FaultEvent):void
		{
			msgStr = event.fault.faultString;
			facade.sendNotification(SAVE_ADDRESS_FAILED, this);
		}
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 연말정산  MainList Value Object
		 */
		/**
		 * 개인정보
		 * 
		 * @param
		 * @return 개인정보 Value Object
		 */
		private function parseAddress(arr:ArrayCollection):AddressVO
		{
			var vo:AddressVO = new AddressVO();
			if(arr.length>0) {
				var rec:Object = arr.getItemAt(0);

				vo.LAND1	= rec["LAND1"];
				vo.LANDX50	= rec["LANDX50"];
				vo.HSHLD	= rec["HSHLD"];
				vo.STATE	= rec["STATE"];
				vo.BEZEI	= rec["BEZEI"];
				vo.PSTLZ	= rec["PSTLZ"];
				vo.ORT1K	= rec["ORT1K"];
				vo.ORT2K	= rec["ORT2K"];
				vo.STRAS	= rec["STRAS"];
				vo.LOCAT	= rec["LOCAT"];
			}
			return vo;
		}
		
	}
}